package test_junit_shop.Customer;

import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import util.BaseDriver;

import java.util.List;

/**
 * Created by Eric on 2018/2/5.
 * 门店-非车主客户
 */
public class NonOwnerCustomer extends BaseDriver {
    private static NonOwnerCustomer noc = new NonOwnerCustomer();
    public static NonOwnerCustomer getInstance(){
        return noc;
    }

    public void add(String customerName, String phoneNum, String address) throws InterruptedException {
        driver.switchTo().defaultContent();
        driver.switchTo().frame("iframeundefined");

        // 点击新增
        driver.findElement(By.xpath("//button[@onclick='add()']")).click();
        Thread.sleep(1000);
        driver.switchTo().frame(0);

        // 姓名
        driver.findElement(By.name("name")).sendKeys(customerName);
        // 性别
        driver.findElement(By.xpath("//select[@name='gender']/option[2]")).click();
        // 手机号
        driver.findElement(By.name("phone")).sendKeys(phoneNum);
        // 地址
        driver.findElement(By.name("address")).sendKeys(address);

        driver.switchTo().parentFrame();
        driver.findElement(By.linkText("确定")).click();
        Thread.sleep(1000);

    }

    public void delete(String customerName) throws InterruptedException {
        driver.switchTo().defaultContent();
        driver.switchTo().frame("iframeundefined");

        // 选择待删除的客户
        List<WebElement> deleteItems = driver.findElements(By.xpath(String.format("//td[contains(text(), '%s')]/preceding-sibling::td[4]/input", customerName)));
        for (WebElement item : deleteItems) {
            item.click();
        }

        // 点击删除
        driver.findElement(By.xpath("//button[@onclick='del();']")).click();
        Thread.sleep(1000);

        // 点击确定
        driver.findElement(By.linkText("确定")).click();

    }
}
